package com.javaee.web.servlet;
import java.io.IOException;
import java.util.Date;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaee.ejb.entity.Conversation;
import com.javaee.ejb.session.remote.ConversationManagerRemote;
import com.javaee.web.HttpServletBase;
import com.javaee.web.SiteParameters;

/**
 * Servlet implementation class AcceptConversationServlet
 */
public class AcceptConversationServlet extends HttpServletBase {
	private static final long serialVersionUID = 1L;

	@EJB
	private ConversationManagerRemote conversationManager;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AcceptConversationServlet() {
		super();
	}

	@Override
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int conversationId = Integer.parseInt(request
				.getParameter("conversationId"));
		Date now = new Date();

		Conversation conversation = conversationManager.getById(conversationId);

		conversation.setStatus(1); // Acceptee
		conversation.setStart(now);

		// Sauvegarde de la conversation
		conversationManager.saveConversation(conversation);

		// Redirection vers la page de gestion du projet
		response.sendRedirect(SiteParameters.getFullServletPath("conversation"));
	}
}